<?php

/**
 * uniBoard Settings Group edit
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-08-15 04:25:27 +0200 (N, 15 sie 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_edit_settings{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Settings group API
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Start settings group API
		$this -> api = new api_settings( $appCore, $appCore -> _REQ( 'group'));
		
		// Did we loaded api from DB?
		if ( $this -> api -> error || !$this -> api -> data_loaded)
		{
			// Draw last error
			$appCore -> output -> error( $appCore -> lang['acp_settings_group_not_found'], $appCore -> lang['acp_settings_groups']);
			
			// Trow user back to groups list
			new action_settings_list( $appCore);	
		}
		else
		{
			// Handle action
			if ( $appCore -> _REQ( 'do') == 'edit')
			{
				// Update action?
				if ( $appCore -> _POST( 'update') == 1)
				{
					// Update api
					$this -> api -> setGroupKey( $appCore -> _POST( 'group_key'));
					$this -> api -> setGroupName( $appCore -> _POST( 'group_name'));
					$this -> api -> setGroupDescription( $appCore -> _POST( 'group_description'));
					
					// Any errors?
					if ( $this -> api -> error)
					{
						// Draw errors
						$appCore -> output -> error( join( '<br />', $this -> api -> errors), $appCore -> ifString( htmlspecialchars( $this -> api -> group_name)));
			
						// Repeat form
						$this -> drawForm();
					}
					else
					{
						// Save data
						$this -> api -> saveData();
						
						// Draw message
						$appCore -> output -> message( $appCore -> lang['acp_settings_edited'], $appCore -> lang['acp_settings_group_edit']);
			
						// Show groups list
						new action_settings_list( $appCore);
					}
				}
				else
				{
					// Draw form
					$this -> drawForm();
				}
			}
			else if ( $appCore -> _REQ( 'do') == 'delete')
			{
				// Delete Group
				$this -> api -> deleteData();
					
				// Draw last error
				$appCore -> output -> message( $appCore -> lang['acp_settings_deleted'], $appCore -> lang['acp_settings_group_delete']);
				
				// Trow user back to groups list
				new action_settings_list( $appCore);
			}
			else
			{
				// Draw last error
				$appCore -> output -> error( $appCore -> lang['wrong_action_code'], $appCore -> lang['acp_settings_groups']);
				
				// Trow user back to groups list
				new action_settings_list( $appCore);
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws form
	 *
	 */
	
	function drawForm(){
		
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Start output buffer
		ob_start();
		
		// Open form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=edit_settings&do=edit&group=' . $this -> api -> group_id));
		
		// Hidden inout
		echo $this -> appCore -> drawHidden( 'update', 1);
		
		// Open block
		echo $this -> appCore -> output -> openFormBlock( $this -> appCore -> ifString( $this -> api -> group_name));

		// Draw fields
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_settings_group_key'], $this -> appCore -> drawTextInput( 'group_key', $this -> api -> group_key), $this -> appCore -> lang['acp_settings_group_key_expl']);
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_settings_group_name'], $this -> appCore -> drawTextInput( 'group_name', $this -> api -> group_name));
		echo $this -> appCore -> output -> formItem( $this -> appCore -> lang['acp_settings_group_description'], $this -> appCore -> drawTextArea( 'group_description', $this -> api -> group_description));
			
		// Draw button
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['save_changes']);
		
		// Close block
		echo $this -> appCore -> output -> closeFormBlock();
		
		// Close form
		echo $this -> appCore -> output -> closeForm();
			
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_settings_group_edit'], array(
			$this -> appCore -> adminLink( 'act=settings_list') => $this -> appCore -> lang['acp_settings_groups'],
			'' => $this -> appCore -> lang['acp_settings_group_edit']
		), ob_get_clean());
	
	}
	
}